using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

namespace GameToolkit
{
    public abstract class NavMeshSource : MonoBehaviour, INavMeshSource
    {
        [SerializeField, NavAreaType]
        int m_AreaType;

        [SerializeField, HideInInspector]
        bool m_IsRuntime;

        bool mRuntimeAdd;

        public int areaType => m_AreaType;
        public Matrix4x4 localToWorldMatrix => transform.localToWorldMatrix;
        public Matrix4x4 worldToLocalMatrix => transform.worldToLocalMatrix;

        public virtual bool AffectOnAgent(int agentType) { return true; }

        public abstract Bounds localBounds { get; }

        public abstract void GetBuildSource(int defaultAreaType, ICollection<NavMeshBuildSource> sources);

        protected virtual void OnEnable()
        {
            mRuntimeAdd = m_IsRuntime;
            if (mRuntimeAdd)
                RuntimeNavMeshBuilder.AddBuildSource(this);
        }

        protected virtual void OnDisable()
        {
            if(mRuntimeAdd)
            {
                mRuntimeAdd = false;
                RuntimeNavMeshBuilder.RemoveBuildSource(this);
            }
        }
    }
}
